package com.emm.yixun.website.controller.system;

//框架包
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.emm.yixun.anotations.MerchantResourcesConfig;
import com.emm.yixun.common.model.dto.*;
import com.emm.yixun.customer.service.CustomerServiceFacade;
import org.activiti.engine.HistoryService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.emm.yixun.common.model.Dictionary;
import com.emm.yixun.common.model.Notice;
import com.emm.yixun.common.model.Project;
import com.emm.yixun.common.model.User;
import com.emm.yixun.common.model.enums.NoticeTypeEnum;
import com.emm.yixun.common.page.Page;
import com.emm.yixun.common.page.PageUtil;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.estate.service.ProjectServiceFacade;
import com.emm.yixun.system.service.DictionaryServiceFacade;
import com.emm.yixun.system.service.NoticeServiceFacade;
import com.emm.yixun.website.common.ajax.AjaxRes;
import com.emm.yixun.website.common.utils.base.Const;
import com.emm.yixun.website.common.utils.security.ShiroUtil;
import com.emm.yixun.website.controller.base.BaseController;
//common
//website
//变化的引用
import com.emm.yixun.website.service.ActivitiProcessServiceImpl;

@Controller
@RequestMapping("/system/notice/")
public class NoticeController extends BaseController<Notice> {

	@Autowired
	private NoticeServiceFacade noticeServiceFacade;
	@Resource
	private DictionaryServiceFacade dictionaryServiceFacade;
	@Resource
	private ProjectServiceFacade projectServiceFacade;
	@Resource
	private CustomerServiceFacade customerServiceFacade;
	@Autowired
	private TaskService taskService; 
	@Autowired
	private HistoryService historyService;
	@Autowired
	private ActivitiProcessServiceImpl activitiProcessServiceImpl;

	@RequestMapping(value = "index")
	public String index(ModelMap map) {
		if (doSecurityIntercept(Const.RESOURCES_TYPE_MENU)) {
			// 查询菜单按钮权限
			map.put("permitBtn", getPermitBtn(Const.RESOURCES_TYPE_FUNCTION));
			return "/system/notice/list";
		}
		return Const.NO_AUTHORIZED_URL;
	}

	// 新增返回实体对象
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes add(NoticeDto dto) {
		AjaxRes ar = getAjaxRes();

		/*
		 * 1.判断权限2.调用新增接口3.返回信息
		 */
		if (ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON, "system:notice:add"))) {
			try {
				User user = ShiroUtil.getCurrentUser();
				dto.setMerchantId(user.getMerchantId());
				dto.setCreateUserId(user.getId());
				dto.setDelFlag("0");
				dto.setUpdateUserId(user.getId());
				dto.setUpdateTime(new Date());
				Response<NoticeDto> response = noticeServiceFacade.save(dto);
				if (response.isSuccess()) {
					ar.setSucceedMsg(Const.SAVE_SUCCEED);
				} else {
					ar.setFailMsg(Const.SAVE_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(), e);
				ar.setFailMsg(Const.SAVE_FAIL);
			}
		}
		return ar;
	}

	// 查看
	@RequestMapping(value = "view", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes view(Long id) {
		AjaxRes ar = getAjaxRes();
		/*
		 * 1.判断权限2.调用查看接口3.返回信息
		 */
		if (ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON, "system:notice:view"))) {
			try {
				Response<Notice> response = noticeServiceFacade.findById(id);
				if (response.isSuccess()) {
					ar.setSucceed(response.getEntity());
				} else {
					ar.setFailMsg(Const.DATA_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(), e);
				ar.setFailMsg(Const.DATA_FAIL);
			}
		}
		return ar;
	}

	// 修改
	@RequestMapping(value = "edit", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes edit(NoticeDto dto) {
		AjaxRes ar = getAjaxRes();
		/*
		 * 1.判断权限2.调用修改接口3.返回信息
		 */
		if (ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON, "system:notice:edit"))) {
			try {
				Response<Integer> response = noticeServiceFacade.updateByPrimaryKey(dto, true);
				if (response.isSuccess()) {
					ar.setSucceedMsg(Const.UPDATE_SUCCEED);
				} else {
					ar.setFailMsg(Const.UPDATE_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(), e);
				ar.setFailMsg(Const.UPDATE_FAIL);
			}
		}
		return ar;
	}

	// 删除
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes delete(Long id) {
		AjaxRes ar = getAjaxRes();
		/*
		 * 1.判断权限2.调用查看接口3.返回信息
		 */
		if (ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON, "system:notice:delete"))) {
			try {
				Response<Integer> response = noticeServiceFacade.deleteByPrimaryKey(id);
				if (response.isSuccess()) {
					ar.setSucceedMsg(Const.DEL_SUCCEED);
				} else {
					ar.setFailMsg(Const.DEL_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(), e);
				ar.setFailMsg(Const.DEL_FAIL);
			}
		}
		return ar;
	}

	// 分页查询
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes list(NoticeDto dto, Integer pageSize, Integer pageNum) {
		AjaxRes ar = getAjaxRes();
		/*
		 * 1.判断权限2.调用分页查询接口3.返回信息
		 */
		if (ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON, "system:notice:list"))) {
			try {
				User user = ShiroUtil.getCurrentUser();
				dto.setMerchantId(user.getMerchantId());
				//公告跟着项目走,采用项目的资源Code
				dto.setResourcesCode("menu_0003_0001");
				Page<Notice> response = noticeServiceFacade.findPageByDto(dto, PageUtil.begin(pageNum, pageSize),
						pageSize);
				if (response.getSuccess()) {
					if (null == response.getRows()) {
						response.setRows(new ArrayList<Notice>());
					} else {
						for (Notice notice : response.getRows()) {
							Response<Dictionary> dicResponse = dictionaryServiceFacade.findById(notice
									.getDictionaryId());
							if (dicResponse.isSuccess() && dicResponse.getEntity() != null) {
								notice.setDicCN(dicResponse.getEntity().getDictionaryName());
							}
							ProjectDto projectDto = new ProjectDto();
							if(StringUtils.isNotEmpty(notice.getProjectCode())){
								projectDto.setProjectCode(notice.getProjectCode());
								ResponseList<Project> proResponseList = projectServiceFacade.findByDto(projectDto);
								if (proResponseList.isSuccess() && proResponseList.getEntity() != null) {
									notice.setProjectCN(proResponseList.getEntity().get(0).getProjectName());
								}
							}
						}
						response.setPageNumber(pageNum);
						ar.setSucceed(response);
					}
				} else {
					ar.setFailMsg(Const.DATA_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(), e);
				ar.setFailMsg(Const.DATA_FAIL);
			}
		}
		return ar;
	}

	/**
	 * 提醒消息列表
	 * 
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "toNoticeList")
	@MerchantResourcesConfig(resourcesCode="menu_0100_0003")
	public String toNoticeList(ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		if (doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"estate:message:list")) {
			// 根据字典值,查询提醒对应的字典Id
			//逾期未签约(160)、逾期未付款(161)、待处理客户(165)、
			//客户维护(162,163,164)、
			//审核结果(183,184,185,186,187,188,189,190,191,192,193,194,195,196)、
			//审核申请(166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182)、
			//客户关怀(197,198,199,200)、
			//员工关怀(201,202,203,204)
			//认筹/认购/签约通知(170,171,172)
			String dicValue = request.getParameter("dicValue");
			long dictionaryId = 0L;
			String dictionaryName = "";
			if("RE01".equals(dicValue)){
				dictionaryId = 160;
				dictionaryName = "逾期未签约";
			}else if("RE02".equals(dicValue)){
				dictionaryId = 161;
				dictionaryName = "逾期未付款";
			}else if("RE03".equals(dicValue)){
				dictionaryId = 165;
				dictionaryName = "待处理客户";
			}else if("RE04".equals(dicValue)){
				dictionaryId = 162;
				dictionaryName = "客户维护";
			}else if("RE05".equals(dicValue)){
				dictionaryId = 183;
				dictionaryName = "审核结果";
			}else if("RE06".equals(dicValue)){
				dictionaryId = 166;
				dictionaryName = "审核申请";
			}else if("RE07".equals(dicValue)){
				dictionaryId = 197;
				dictionaryName = "客户关怀";
			}else if("RE08".equals(dicValue)){
				dictionaryId = 201;
				dictionaryName = "员工关怀";
			}else if("RE09".equals(dicValue)){
				dictionaryId = 170;
				dictionaryName = "认筹/认购/签约通知";
			}

			// 查询菜单按钮权限
			//map.put("permitBtn", getPermitBtn(Const.RESOURCES_TYPE_FUNCTION));
			map.put("dicId", dictionaryId);
			map.put("dicName", dictionaryName);
			return "/system/notice/list";
		}
		return Const.NO_AUTHORIZED_URL;
	}

		//绿城通知页面修改
	@RequestMapping(value = "toNoticeListLC")
	public String toNoticeListLC(ModelMap map, HttpServletRequest request) {
		if (doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"estate:message:list")) {
			// 根据字典值,查询提醒对应的字典Id
			//逾期未签约(160)、逾期未付款(161)、待处理客户(165)、
			//客户维护(162,163,164)、
			//审核结果(183,184,185,186,187,188,189,190,191,192,193,194,195,196)、
			//审核申请(166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182)、
			//客户关怀(197,198,199,200)、
			//员工关怀(201,202,203,204)
			//认筹/认购/签约通知(170,171,172)
			String dicValue = request.getParameter("dicValue");
			long dictionaryId = 0L;
			String dictionaryName = "";
			if("RE01".equals(dicValue)){
				dictionaryId = 160;
				dictionaryName = "逾期未签约";
			}else if("RE02".equals(dicValue)){
				dictionaryId = 161;
				dictionaryName = "逾期未付款";
			}else if("RE03".equals(dicValue)){
				dictionaryId = 165;
				dictionaryName = "待处理客户";
			}else if("RE04".equals(dicValue)){
				dictionaryId = 162;
				dictionaryName = "客户维护";
			}else if("RE05".equals(dicValue)){
				dictionaryId = 183;
				dictionaryName = "审核结果";
			}else if("RE06".equals(dicValue)){
				dictionaryId = 166;
				dictionaryName = "审核申请";
			}else if("RE07".equals(dicValue)){
				dictionaryId = 197;
				dictionaryName = "客户关怀";
			}else if("RE08".equals(dicValue)){
				dictionaryId = 201;
				dictionaryName = "员工关怀";
			}else if("RE09".equals(dicValue)){
				dictionaryId = 170;
				dictionaryName = "认筹/认购/签约通知";
			}

			// 查询菜单按钮权限
			//map.put("permitBtn", getPermitBtn(Const.RESOURCES_TYPE_FUNCTION));
			map.put("dicId", dictionaryId);
			map.put("dicName", dictionaryName);
			return "/system/notice/listLC";
		}
		return Const.NO_AUTHORIZED_URL;
	}

	// 分页查询
	@RequestMapping(value = "queryNoticeList", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes queryNoticeList(String dictionaryName, Integer pageSize, Integer pageNum,HttpServletRequest request) {
		AjaxRes ar = getAjaxRes();
		/*
		 * 1.判断权限2.调用分页查询接口3.返回信息
		 */
		if (ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON, "estate:message:list"))) {
			try {
				User user = ShiroUtil.getCurrentUser();
				ReceiverNoticeDto receiverNoticeDto = new ReceiverNoticeDto();
				ReceiverNoticeDto addDto = null;
				receiverNoticeDto.setMerchantId(user.getMerchantId());
				receiverNoticeDto.setReceiverUserId(user.getId());
				if(StringUtils.isNotBlank(dictionaryName)){
				   receiverNoticeDto.setDictionaryIds(findTXByRemark(dictionaryName));
				}
				if(!"员工关怀".equals(dictionaryName)){
					receiverNoticeDto.setReceiverUserId(user.getId());
				}
				//通知类型为提醒
				receiverNoticeDto.setNoticeType(NoticeTypeEnum.REMIND.getNo());
				Page<ReceiverNoticeDto> response = noticeServiceFacade.receiverNoticePage(receiverNoticeDto, PageUtil.begin(pageNum, pageSize), pageSize);
				if (response.getSuccess()) {
					//if(dictionaryName.equals("审核申请")){
						//查询待办
						if(null!=response.getRows()){
							for(ReceiverNoticeDto nrd : response.getRows()){
								if(StringUtils.isNotBlank(nrd.getProjectCode())){
									ProjectDto dto = new ProjectDto();
									dto.setProjectCode(nrd.getProjectCode());
									ResponseList<Project> res = projectServiceFacade.findByDto(dto);
									if(res.isSuccess() && res.getEntity() != null){
										nrd.setProjectId(res.getEntity().get(0).getId()+"");
									}
								}
								
								if(StringUtils.isBlank(nrd.getRemark()))continue;
								JSONObject object=JSON.parseObject(nrd.getRemark());
								String nextNodeId=object.getString("nextNodeId");
								String processInstanceId=object.getString("processInstanceId");
								String businessId=object.getString("businessId");
								String customerId=object.getString("customerId");
								/*if(StringUtils.isBlank(nextNodeId) || StringUtils.isBlank(processInstanceId))continue;
								HistoricProcessInstance historicProcessInstance=historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
								if(null!=historicProcessInstance){
									nrd.setBusinessKey(historicProcessInstance.getBusinessKey());
								}
								Task cruTask=taskService.createTaskQuery().processInstanceId(processInstanceId).taskDefinitionKey(nextNodeId).singleResult();
								if(null==cruTask){
									nrd.setTaskId("");
									nrd.setFormKey(activitiProcessServiceImpl.getFormKeyByInstanceId(processInstanceId));
								}else{
									nrd.setTaskId(cruTask.getId());
									nrd.setFormKey(cruTask.getFormKey());
								}*/
								if(StringUtils.isNotBlank(processInstanceId)){
									HistoricProcessInstance historicProcessInstance=historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
									if(null!=historicProcessInstance){
										nrd.setBusinessKey(historicProcessInstance.getBusinessKey());
									}
									Task cruTask=taskService.createTaskQuery().processInstanceId(processInstanceId).taskDefinitionKey(nextNodeId).singleResult();
									if(null==cruTask){
										nrd.setTaskId("");
										nrd.setFormKey(activitiProcessServiceImpl.getFormKeyByInstanceId(processInstanceId) + "?procInstId=" + processInstanceId);
									}else{
										nrd.setTaskId(cruTask.getId());
										nrd.setFormKey(cruTask.getFormKey());
									}
								}
								if(StringUtils.isNoneBlank(businessId)){
									nrd.setBusinessId(businessId);
								}
								
								if(StringUtils.isNotBlank(customerId)){
									nrd.setCustomerId(customerId);
								}
							}
						//}
					}

					response.setPageNumber(pageNum);
					ar.setSucceed(response);
				} else {
					ar.setFailMsg(Const.DATA_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(), e);
				ar.setFailMsg(Const.DATA_FAIL);
			}
		}
		return ar;
	}


	// 分页查询
	@RequestMapping(value = "queryNoticeListLC", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes queryNoticeListLC(String dictionaryName, Integer pageSize, Integer pageNum,HttpServletRequest request) {
		AjaxRes ar = getAjaxRes();
		/*
		 * 1.判断权限2.调用分页查询接口3.返回信息
		 */
		if (ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON, "estate:message:list"))) {
			try {
				User user = ShiroUtil.getCurrentUser();
				ReceiverNoticeDto receiverNoticeDto = new ReceiverNoticeDto();
				ReceiverNoticeDto addDto = null;
				receiverNoticeDto.setMerchantId(user.getMerchantId());
				receiverNoticeDto.setReceiverUserId(user.getId());
				if(StringUtils.isNotBlank(dictionaryName)){
					receiverNoticeDto.setDictionaryIds(findTXByRemark(dictionaryName));
				}
				if(!"员工关怀".equals(dictionaryName)){
					receiverNoticeDto.setReceiverUserId(user.getId());
				}
				int count = 0;
				if("待处理客户".equals(dictionaryName)){
					//查询完善客户信息
					CustomerComponentDto dto = new CustomerComponentDto();
					//	dto.setUserCode(user.getUserCode());
					dto = setCustValue(dto);
					count  = customerServiceFacade.findIntentionIntactCount(dto);
				}
				if(count > 0 && pageNum ==1){//首页将这条数据加进去
					pageSize -=1;
					addDto = new ReceiverNoticeDto();
					addDto.setUserCode(user.getUserCode());
					//improvenow =1 进入客户列表页面显示 7天之内未完成的用户
					addDto.setNoticeTypeName("完善客户信息");
					addDto.setContent("您有<span style='color:#c00'>" +count+ "</span>个客户信息等待完善，<a href ='"+request.getContextPath()+"/customer/customer/index?menu=menu_0006_0001&improvenow=1' >立即完善！</a>");
					addDto.setIssueTime(new Date());//发生时间
				}
				//通知类型为提醒
				receiverNoticeDto.setNoticeType(NoticeTypeEnum.REMIND.getNo());
				Page<ReceiverNoticeDto> response = noticeServiceFacade.receiverNoticePage(receiverNoticeDto, PageUtil.begin(pageNum, pageSize), pageSize);
				if (response.getSuccess()) {
					//if(dictionaryName.equals("审核申请")){
					//查询待办
					if(null!=response.getRows()){
						for(ReceiverNoticeDto nrd : response.getRows()){
							if(StringUtils.isNotBlank(nrd.getProjectCode())){
								ProjectDto dto = new ProjectDto();
								dto.setProjectCode(nrd.getProjectCode());
								ResponseList<Project> res = projectServiceFacade.findByDto(dto);
								if(res.isSuccess() && res.getEntity() != null){
									nrd.setProjectId(res.getEntity().get(0).getId()+"");
								}
							}

							if(StringUtils.isBlank(nrd.getRemark()))continue;
							JSONObject object=JSON.parseObject(nrd.getRemark());
							String nextNodeId=object.getString("nextNodeId");
							String processInstanceId=object.getString("processInstanceId");
							String businessId=object.getString("businessId");
							String customerId=object.getString("customerId");
								/*if(StringUtils.isBlank(nextNodeId) || StringUtils.isBlank(processInstanceId))continue;
								HistoricProcessInstance historicProcessInstance=historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
								if(null!=historicProcessInstance){
									nrd.setBusinessKey(historicProcessInstance.getBusinessKey());
								}
								Task cruTask=taskService.createTaskQuery().processInstanceId(processInstanceId).taskDefinitionKey(nextNodeId).singleResult();
								if(null==cruTask){
									nrd.setTaskId("");
									nrd.setFormKey(activitiProcessServiceImpl.getFormKeyByInstanceId(processInstanceId));
								}else{
									nrd.setTaskId(cruTask.getId());
									nrd.setFormKey(cruTask.getFormKey());
								}*/
							if(StringUtils.isNotBlank(processInstanceId)){
								HistoricProcessInstance historicProcessInstance=historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
								if(null!=historicProcessInstance){
									nrd.setBusinessKey(historicProcessInstance.getBusinessKey());
								}
								Task cruTask=taskService.createTaskQuery().processInstanceId(processInstanceId).taskDefinitionKey(nextNodeId).singleResult();
								if(null==cruTask){
									nrd.setTaskId("");
									nrd.setFormKey(activitiProcessServiceImpl.getFormKeyByInstanceId(processInstanceId) + "?procInstId=" + processInstanceId);
								}else{
									nrd.setTaskId(cruTask.getId());
									nrd.setFormKey(cruTask.getFormKey());
								}
							}
							if(StringUtils.isNoneBlank(businessId)){
								nrd.setBusinessId(businessId);
							}

							if(StringUtils.isNotBlank(customerId)){
								nrd.setCustomerId(customerId);
							}
						}
						//}
					}

					//客户信息完善加入到列表里
					// todo 在返回内容中加入链接 进入客户列表页面显示只有客户信息与意向等级信息不完善的用户
					if(addDto != null){
						if(null != response.getRows()) {
							response.getRows().add(addDto);
						}else{
							List<ReceiverNoticeDto> list = new ArrayList<ReceiverNoticeDto>();
							list.add(addDto);
							response.setRows(list);
						}
					}
					response.setPageNumber(pageNum);
					ar.setSucceed(response);
				} else {
					ar.setFailMsg(Const.DATA_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(), e);
				ar.setFailMsg(Const.DATA_FAIL);
			}
		}
		return ar;
	}

	
	private List<Long> findTXByRemark(String remark){
		List<Long> ids=new ArrayList<Long>();
		DictionaryDto dicDto = new DictionaryDto();
		dicDto.setRemark(remark);
		ResponseList<Dictionary> dicListRes = dictionaryServiceFacade.findByDto(dicDto);
		if(dicListRes.isSuccess() && null!=dicListRes.getEntity()){
			for(Dictionary  dictionary : dicListRes.getEntity()){
				ids.add(dictionary.getId());
			}
		}else{
			//如果类型不存在，则默认-999  即不能查到该消息类型
			ids.add(Long.valueOf(-999));
		}
		return ids;
	}
	@RequestMapping(value = "paymentPush")
	public @ResponseBody AjaxRes test(){
		noticeServiceFacade.customerNoSignedNotice();
		noticeServiceFacade.customerNoSignedPushMsgCurr();
		return getAjaxRes();
	}

	private CustomerComponentDto setCustValue(CustomerComponentDto dto) {
		if (dto.getFtContactTimeAfter() != null && !dto.getFtContactTimeAfter().equals("")) {
			dto.setFtContactTimeAfter(dto.getFtContactTimeAfter() + " 23:59:59");
		}
		//最新跟进时间
		if (StringUtils.isNotBlank(dto.getLastFollowTimeBefore())) {
			dto.setLastFollowTimeBefore(dto.getLastFollowTimeBefore() + " 00:00:00");
		}
		if (StringUtils.isNotBlank(dto.getLastFollowTimeAfter())) {
			dto.setLastFollowTimeAfter(dto.getLastFollowTimeAfter() + " 23:59:59");
		}
		//录入时间（即为业务创建时间）
		if (StringUtils.isNotBlank(dto.getCreateTimeBefore())) {
			dto.setCreateTimeBefore(dto.getCreateTimeBefore() + " 00:00:00");
		}

		if (StringUtils.isNotBlank(dto.getCreateTimeAfter())) {
			dto.setCreateTimeAfter(dto.getCreateTimeAfter() + " 23:59:59");
		}
		if (dto.getUserCode() != null && dto.getUserCode().equals("-1")) {
			dto.setUserCode(null);
		}

		// 数据权限
		User user = ShiroUtil.getCurrentUser();
		//dto.setPermissionUserId(user.getId());
		dto.setUserCode(user.getUserCode());
		dto.setMerchantId(user.getMerchantId());
		dto.setOrders(" t.create_time desc ");
		return dto;
	}
}
